window.onload = function() {
    $('.treeInput').click(function() {
        $(this).parent().parent().siblings().show();
    });

    layui.extend({
        dtree: '{/}layui_ext/dtree/dtree' // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['dtree', 'layer', 'jquery'], function() {
        var dtree = layui.dtree,
            layer = layui.layer,
            $ = layui.jquery;

        // 初始化树
        var DemoTree = dtree.render({
            elem: "#demoTree",
            data: demoData, // 使用data加载
            // menubar: true,
            icon: "-1", // 隐藏二级图标
            initLevel: "1",
            line: true,
            url: "../json/case/demoJson.json" // 使用url加载（可与data加载同时存在）
        }, );

        // 绑定节点点击
        // dtree.on("node('demoTree')", function(obj) {
        //     layer.msg(JSON.stringify(obj.param));
        // });
        $('#confirm').click(function() {
            var param = dtree.getNowParam("demoTree"); //获取选中的值
            // console.log(param);
            if ($.isEmptyObject(param)) {
                return true;
            } else {
                $('.treeInput').val(param.context);
                $('.selectBox').hide();
            }
        })
        $('#cancel').click(function() {
            $('.selectBox').hide();
        })
    });
    //数据格式
    var demoData = [{
            "id": "003",
            "title": "中国",
            "isLast": false,
            "parentId": "0",
            "level": "1",
            "checkArr": [{
                "type": "0",
                "isChecked": "0"
            }],
            "children": [{
                "id": "003",
                "title": "广东省",
                "isLast": false,
                "parentId": "0",
                "level": "1",
                "checkArr": [{
                    "type": "0",
                    "isChecked": "0"
                }],
                "children": [{
                    "id": "003001",
                    "title": "广州市",
                    "isLast": false,
                    "parentId": "003",
                    "level": "2",
                    "checkArr": [{
                        "type": "0",
                        "isChecked": "0"
                    }],
                    "children": [{
                        "id": "003001001",
                        "title": "天河区",
                        "isLast": true,
                        "parentId": "003001",
                        "checkArr": [{
                            "type": "0",
                            "isChecked": "0"
                        }],
                        "level": "3",
                        "children": [{
                            "id": "001",
                            "title": "湖南省",
                            "isLast": false,
                            "level": "1",
                            "parentId": "0",
                            "checkArr": [{
                                "type": "0",
                                "isChecked": "0"
                            }],
                            "basicData": {
                                "data1": "自定义数据111",
                                "data2": "自定义数据222",
                                "data3": "自定义'我带了单引号'333"
                            },
                            "children": [{
                                "id": "001001",
                                "title": "长沙市",
                                "isLast": true,
                                "parentId": "001",
                                "checkArr": [{
                                    "type": "0",
                                    "isChecked": "0"
                                }],
                                "level": "2"
                            }, {
                                "id": "001002",
                                "title": "株洲市",
                                "isLast": true,
                                "parentId": "001",
                                "checkArr": [{
                                    "type": "0",
                                    "isChecked": "0"
                                }],
                                "level": "2"
                            }, {
                                "id": "001003",
                                "title": "湘潭市",
                                "isLast": true,
                                "parentId": "001",
                                "checkArr": [{
                                    "type": "0",
                                    "isChecked": "0"
                                }],
                                "level": "2"
                            }, {
                                "id": "001004",
                                "title": "衡阳市",
                                "isLast": true,
                                "parentId": "001",
                                "checkArr": [{
                                    "type": "0",
                                    "isChecked": "0"
                                }],
                                "level": "2"
                            }, {
                                "id": "001005",
                                "title": "郴州市",
                                "isLast": true,
                                "parentId": "001",
                                "checkArr": [{
                                    "type": "0",
                                    "isChecked": "0"
                                }],
                                "level": "2",
                                "children": [{
                                    "id": "002",
                                    "title": "湖北省",
                                    "isLast": false,
                                    "parentId": "0",
                                    "level": "1",
                                    "checkArr": [{
                                        "type": "0",
                                        "isChecked": "0"
                                    }],
                                    "children": [{
                                        "id": "002001",
                                        "title": "武汉市",
                                        "isLast": true,
                                        "checkArr": [{
                                            "type": "0",
                                            "isChecked": "0"
                                        }],
                                        "level": "2"
                                    }, {
                                        "id": "002002",
                                        "title": "黄冈市",
                                        "checkArr": [{
                                            "type": "0",
                                            "isChecked": "0"
                                        }],
                                        "isLast": true,
                                        "parentId": "002",
                                        "level": "2"
                                    }, {
                                        "id": "002003",
                                        "title": "潜江市",
                                        "isLast": true,
                                        "parentId": "002",
                                        "checkArr": [{
                                            "type": "0",
                                            "isChecked": "0"
                                        }],
                                        "level": "2"
                                    }, {
                                        "id": "002004",
                                        "title": "荆州市",
                                        "isLast": true,
                                        "parentId": "002",
                                        "checkArr": [{
                                            "type": "0",
                                            "isChecked": "0"
                                        }],
                                        "level": "2"
                                    }, {
                                        "id": "002005",
                                        "title": "襄阳市",
                                        "isLast": true,
                                        "parentId": "002",
                                        "checkArr": [{
                                            "type": "0",
                                            "isChecked": "0"
                                        }],
                                        "level": "2",
                                        "children": [{
                                            "id": "005",
                                            "title": "福建省",
                                            "isLast": false,
                                            "parentId": "0",
                                            "level": "1",
                                            "checkArr": [{
                                                "type": "0",
                                                "isChecked": "0"
                                            }],
                                            "children": [{
                                                "id": "005001",
                                                "title": "厦门市",
                                                "isLast": true,
                                                "parentId": "005",
                                                "checkArr": [{
                                                    "type": "0",
                                                    "isChecked": "0"
                                                }],
                                                "level": "2",
                                                "children": [{
                                                    "id": "004",
                                                    "title": "浙江省",
                                                    "isLast": false,
                                                    "level": "1",
                                                    "parentId": "0",
                                                    "checkArr": [{
                                                        "type": "0",
                                                        "isChecked": "0"
                                                    }],
                                                    "children": [{
                                                        "id": "004001",
                                                        "title": "杭州市",
                                                        "isLast": true,
                                                        "parentId": "004",
                                                        "checkArr": [{
                                                            "type": "0",
                                                            "isChecked": "0"
                                                        }],
                                                        "level": "2"
                                                    }, {
                                                        "id": "004002",
                                                        "title": "温州市",
                                                        "isLast": true,
                                                        "parentId": "004",
                                                        "checkArr": [{
                                                            "type": "0",
                                                            "isChecked": "0"
                                                        }],
                                                        "level": "2"
                                                    }, {
                                                        "id": "004003",
                                                        "title": "绍兴市",
                                                        "isLast": true,
                                                        "parentId": "004",
                                                        "checkArr": [{
                                                            "type": "0",
                                                            "isChecked": "0"
                                                        }],
                                                        "level": "2"
                                                    }, {
                                                        "id": "004004",
                                                        "title": "金华市",
                                                        "isLast": true,
                                                        "parentId": "004",
                                                        "checkArr": [{
                                                            "type": "0",
                                                            "isChecked": "0"
                                                        }],
                                                        "level": "2"
                                                    }, {
                                                        "id": "004005",
                                                        "title": "义乌市",
                                                        "isLast": true,
                                                        "parentId": "004",
                                                        "checkArr": [{
                                                            "type": "0",
                                                            "isChecked": "0"
                                                        }],
                                                        "level": "2"
                                                    }]
                                                }, ]
                                            }]
                                        }]
                                    }]
                                }, ]
                            }]
                        }, ]
                    }, {
                        "id": "003001002",
                        "title": "花都区",
                        "isLast": true,
                        "parentId": "003001",
                        "checkArr": [{
                            "type": "0",
                            "isChecked": "0"
                        }],
                        "level": "3"
                    }]
                }, {
                    "id": "003002",
                    "title": "深圳市",
                    "isLast": true,
                    "parentId": "003",
                    "checkArr": [{
                        "type": "0",
                        "isChecked": "0"
                    }],
                    "level": "2"
                }, {
                    "id": "003003",
                    "title": "中山市",
                    "isLast": true,
                    "parentId": "003",
                    "checkArr": [{
                        "type": "0",
                        "isChecked": "0"
                    }],
                    "level": "2"
                }, {
                    "id": "003004",
                    "title": "东莞市",
                    "isLast": true,
                    "parentId": "003",
                    "checkArr": [{
                        "type": "0",
                        "isChecked": "0"
                    }],
                    "level": "2"
                }, {
                    "id": "003005",
                    "title": "珠海市",
                    "isLast": true,
                    "parentId": "003",
                    "checkArr": [{
                        "type": "0",
                        "isChecked": "0"
                    }],
                    "level": "2"
                }, {
                    "id": "003006",
                    "title": "韶关市",
                    "isLast": true,
                    "parentId": "003",
                    "checkArr": [{
                        "type": "0",
                        "isChecked": "0"
                    }],
                    "level": "2"
                }]
            }, ]
        },

    ]
}